/**
 * 数据合并使用
 */
export function merge(option: object, data: object) {
  const newOption = JSON.parse(JSON.stringify(option), function (key, item) {
    if (
      typeof item === "string" &&
      (/^(data|dataJson)\./.test(item) || /^(window)\./.test(item))
    ) {
      const fields = item.split(".");
      if (fields[0] === "dataJson") {
        fields[0] = "data";
      }
      return fields.reduce(
        function (current, item, index) {
          try {
            if (index === 0) {
              return current;
            }
            return current[item as keyof typeof current];
          } catch (e) {
            return {};
          }
        },
        fields[0] === "window" ? window : data
      );
    }
    return item;
  });
  return newOption;
}
